package com.yang.demo.config;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置管理器
 * 负责加载和管理RestClient的所有配置项
 */
@Slf4j
public class ConfigManager {

    private static final String CONFIG_FILE = "config.properties";
    private static ConfigManager instance;
    private Properties properties;

    private ConfigManager() {
        loadProperties();
    }

    /**
     * 获取ConfigManager单例实例
     */
    public static synchronized ConfigManager getInstance() {
        if (instance == null) {
            instance = new ConfigManager();
        }
        return instance;
    }

    /**
     * 加载配置文件
     */
    private void loadProperties() {
        properties = new Properties();
        try (InputStream input = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) {
            if (input == null) {
                log.warn("配置文件 {} 未找到，使用默认配置", CONFIG_FILE);
                loadDefaultProperties();
                return;
            }
            properties.load(input);
            log.info("成功加载配置文件: {}", CONFIG_FILE);
        } catch (IOException e) {
            log.error("加载配置文件失败: {}", e.getMessage(), e);
            loadDefaultProperties();
        }
    }

    /**
     * 加载默认配置
     */
    private void loadDefaultProperties() {
        properties.setProperty("connect.timeout", "5000");
        properties.setProperty("socket.timeout", "10000");
        properties.setProperty("connection.request.timeout", "3000");
        properties.setProperty("total.timeout", "15000");
        properties.setProperty("proxy.enabled", "false");
        properties.setProperty("proxy.host", "127.0.0.1");
        properties.setProperty("proxy.port", "8080");
        properties.setProperty("default.proxy.enabled", "false");
        properties.setProperty("default.proxy.host", "127.0.0.1");
        properties.setProperty("default.proxy.port", "8888");
        properties.setProperty("connection.pool.max.total", "200");
        properties.setProperty("connection.pool.default.max.per.route", "50");
        properties.setProperty("connection.pool.validate.after.inactivity", "2000");
        properties.setProperty("retry.count", "3");
        properties.setProperty("retry.interval", "1000");
        properties.setProperty("ssl.trust.all.certificates", "false");
        properties.setProperty("ssl.hostname.verification.enabled", "true");
        properties.setProperty("default.charset", "UTF-8");
        properties.setProperty("user.agent", "RestClient/1.0");
        properties.setProperty("compression.enabled", "true");
        properties.setProperty("accept.encoding", "gzip,deflate");
        properties.setProperty("logging.enabled", "true");
        properties.setProperty("log.request.headers", "true");
        properties.setProperty("log.response.headers", "true");
    }

    // 超时配置
    public int getConnectTimeout() {
        return Integer.parseInt(properties.getProperty("connect.timeout", "5000"));
    }

    public int getSocketTimeout() {
        return Integer.parseInt(properties.getProperty("socket.timeout", "10000"));
    }

    public int getConnectionRequestTimeout() {
        return Integer.parseInt(properties.getProperty("connection.request.timeout", "3000"));
    }

    public int getTotalTimeout() {
        return Integer.parseInt(properties.getProperty("total.timeout", "15000"));
    }

    // 代理配置
    public boolean isProxyEnabled() {
        return Boolean.parseBoolean(properties.getProperty("proxy.enabled", "false"));
    }

    public String getProxyHost() {
        return properties.getProperty("proxy.host", "127.0.0.1");
    }

    public int getProxyPort() {
        return Integer.parseInt(properties.getProperty("proxy.port", "8080"));
    }

    public String getProxyUsername() {
        return properties.getProperty("proxy.username", "");
    }

    public String getProxyPassword() {
        return properties.getProperty("proxy.password", "");
    }

    // 默认代理配置
    public boolean isDefaultProxyEnabled() {
        return Boolean.parseBoolean(properties.getProperty("default.proxy.enabled", "false"));
    }

    public String getDefaultProxyHost() {
        return properties.getProperty("default.proxy.host", "127.0.0.1");
    }

    public int getDefaultProxyPort() {
        return Integer.parseInt(properties.getProperty("default.proxy.port", "8888"));
    }

    public String getDefaultProxyUsername() {
        return properties.getProperty("default.proxy.username", "");
    }

    public String getDefaultProxyPassword() {
        return properties.getProperty("default.proxy.password", "");
    }

    // 连接池配置
    public int getConnectionPoolMaxTotal() {
        return Integer.parseInt(properties.getProperty("connection.pool.max.total", "200"));
    }

    public int getConnectionPoolDefaultMaxPerRoute() {
        return Integer.parseInt(properties.getProperty("connection.pool.default.max.per.route", "50"));
    }

    public int getConnectionPoolValidateAfterInactivity() {
        return Integer.parseInt(properties.getProperty("connection.pool.validate.after.inactivity", "2000"));
    }

    // 重试配置
    public int getRetryCount() {
        return Integer.parseInt(properties.getProperty("retry.count", "3"));
    }

    public int getRetryInterval() {
        return Integer.parseInt(properties.getProperty("retry.interval", "1000"));
    }

    // SSL配置
    public boolean isTrustAllCertificates() {
        return Boolean.parseBoolean(properties.getProperty("ssl.trust.all.certificates", "false"));
    }

    public boolean isHostnameVerificationEnabled() {
        return Boolean.parseBoolean(properties.getProperty("ssl.hostname.verification.enabled", "true"));
    }

    // 其他配置
    public String getDefaultCharset() {
        return properties.getProperty("default.charset", "UTF-8");
    }

    public String getUserAgent() {
        return properties.getProperty("user.agent", "RestClient/1.0");
    }

    public boolean isCompressionEnabled() {
        return Boolean.parseBoolean(properties.getProperty("compression.enabled", "true"));
    }

    public String getAcceptEncoding() {
        return properties.getProperty("accept.encoding", "gzip,deflate");
    }

    // 日志配置
    public boolean isLoggingEnabled() {
        return Boolean.parseBoolean(properties.getProperty("logging.enabled", "true"));
    }

    public boolean isLogRequestHeaders() {
        return Boolean.parseBoolean(properties.getProperty("log.request.headers", "true"));
    }

    public boolean isLogResponseHeaders() {
        return Boolean.parseBoolean(properties.getProperty("log.response.headers", "true"));
    }

    /**
     * 重新加载配置文件
     */
    public void reload() {
        loadProperties();
        log.info("配置文件已重新加载");
    }

    /**
     * 获取所有配置属性（用于调试）
     */
    public Properties getAllProperties() {
        return new Properties(properties);
    }
}